/**
 * 
 */
package com.zznode.client.wlanpolling.model;

import java.io.ByteArrayInputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.xml.xpath.XPathConstants;

import org.w3c.dom.NodeList;

import android.util.Log;

import com.farproc.wifi.connecter.NewNetworkContent;
import com.zznode.client.wlanpolling.service.net.EnumHttpMethod;
import com.zznode.client.wlanpolling.service.net.HttpCallback;
import com.zznode.client.wlanpolling.service.net.HttpException;
import com.zznode.client.wlanpolling.service.net.HttpExecutorFactory;
import com.zznode.client.wlanpolling.service.net.HttpGetExecutor;
import com.zznode.client.wlanpolling.service.net.HttpParam;
import com.zznode.client.wlanpolling.service.xml.XmlParserException;
import com.zznode.client.wlanpolling.service.xml.XmlXPathAdaptor;
import com.zznode.client.wlanpolling.utils.WLanPollingUtils;
import com.zznode.global.Global;

/**
 * 
 *
 */
public class LoadDicts implements HttpCallback {

	private final String TAG = getClass().getSimpleName();

	private String path = null;
	private XmlXPathAdaptor xPath = null;
	private String BUSINTYPEID = null;
	private List<String> list = new ArrayList<String>();
	private Map<String, String>map = new HashMap<String, String>();

	public void retrieveResInfo(String BUSINTYPEID) {
		this.BUSINTYPEID = BUSINTYPEID;
		try {
			HttpGetExecutor executor = (HttpGetExecutor) HttpExecutorFactory
					.createHttpExecutor(EnumHttpMethod.GET);
			executor.setUrl(WLanPollingUtils.getDictsURL());
			executor.addParam(new HttpParam("dictTypeId", BUSINTYPEID));
			executor.asyncExecute(this, 0);
		} catch (Exception e) {
			// TODO: handle exception
		}
	}

	public void setPath(String path) {
		this.path = path;
	}
	@Override
	public void onReceiveData(byte[] data, final HttpException exp, int tag) {
	
			if (exp == null) {
				try {
					xPath = new XmlXPathAdaptor(new ByteArrayInputStream(data));
					NodeList allList = null;
					try {
						 allList = (NodeList)xPath.evaluate(path + "BUSINID", XPathConstants.NODESET);
					} catch (XmlParserException e) {
						Log.e(TAG, e.getMessage());
					}
					list.clear();
					map.clear();
					for(int i=0; i < allList.getLength(); i++){
						//节点属性name的值
						String nodeNameValue = allList.item(i).getAttributes().getNamedItem("name").getTextContent();
						String nodeValue = allList.item(i).getTextContent();
						map.put(nodeNameValue, nodeValue);
						list.add(nodeNameValue);
						System.err.println("-------------------------nodeNameValue:" +nodeNameValue+":nodeValue:"+nodeValue);
					}
					if(BUSINTYPEID.equals("BNDICT_hotspotsovtech")){
						Global.hotspotsovtechMap = map;
						Global.hotspotsovtechList = list;
					}
					 else if(BUSINTYPEID.equals("BNDICT_transmissiontype")){
						 Global.transmissiontypeMap = map;
						 Global.transmissiontypeList = list;
					 }
					 else if(BUSINTYPEID.equals("BNDICT_hotspottype")){
						 Global.hotspottypeMap = map;
						 Global.hotspottypeList = list;
					 }
					 else if(BUSINTYPEID.equals("BNDICT_schoolsubclass")){
						 Global.maplist.put("学校", list);
						 Global.map.put("学校", map);
					 }
					 else if(BUSINTYPEID.equals("BNDICT_hospitalsubclass")){
						 Global.maplist.put("医院", list);
						 Global.map.put("医院", map);
					 }
					 else if(BUSINTYPEID.equals("BNDICT_hotalsubclass")){
						 Global.maplist.put("宾馆酒店", list);
						 Global.map.put("宾馆酒店", map);
					 }
					 else if(BUSINTYPEID.equals("BNDICT_is_cover_by_ap")){
						 Global.iscoverbyapMap = map;
						 Global.iscoverbyap = list;
					 }
	
				} catch (final Exception e) {
//							Toast.makeText(getApplicationContext(), e.getMessage(),
//									Toast.LENGTH_LONG).show();
							System.err.println(e);
						}
				}

		} 
}